package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lyd
 * @Date:2023/8/16 13:59
 * @Version:1.0
 * @Description
 **/

@RestController
@RequestMapping("/admin/product")
public class SpuController {


    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    /**
     * 分页查询SPU属性
     * @param page
     * @param limit
     * @param c3Id
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuPage(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit,
                             @RequestParam("category3Id") Long c3Id){
        Page<SpuInfo> pages = spuInfoService.lambdaQuery()
                .eq(SpuInfo::getCategory3Id, c3Id)
                .page(new Page<>(page, limit));
        return Result.ok(pages).message("成功");
    }



    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SaveSpuVo vo) {
        spuInfoService.saveSpuInfo(vo);
        return Result.ok().message("成功");
    }

    /**
     * 根据spuId获取图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        List<SpuImage> list = spuImageService.lambdaQuery()
                .eq(SpuImage::getSpuId, spuId)
                .list();
        return Result.ok(list).message("成功");
    }


}
